/*
 * pi.cpp
 * $Id: pi.cpp 4 2008-09-10 08:12:24Z clsung $
 */
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "mytime.h"

using namespace std;
double pi, w;
const int interval = 100000000;

double f(double a)
{
    return (4.0 / (1.0 + a*a));
}

void *PIworker()
{
    double sum = 0.0, x;
    for (int i = 1; i <= interval; ++i) {
        x = w*(static_cast<double>(i) - 0.5);
        sum += f (x);
    }
    pi = w * sum;
    return NULL;
}

int main()
{
    clock_t s_c, e_c;
    int s_t, e_t;
    cerr << endl << "Single CPU/Thread" << endl;
    s_t = myclock();
    s_c = clock();
    w = 1.0 / static_cast<double>(interval);
    pi = 0.0;
    PIworker();
    e_t = myclock();
    e_c = clock();
    cerr << "pi = " << fixed << pi << endl;
    cerr << "cost " << (static_cast<double>(e_c - s_c) / CLOCKS_PER_SEC ) << " Sec." << endl;
    cerr << "cost " << (static_cast<double>(e_t - s_t) / 1000000) << " Sec." << endl;
    return 0;
}
